/*
 * Board game - put the figures down and try to make a line.
 * Copyright (C)  2008 Georgi Staykov
 * 
 * This file is part of ClearTheLines.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.gstaykov.clearthelines.figures;

import com.gstaykov.clearthelines.ImageMatrix;

public class Figure4 extends AbstractFigure {

/* rotPos 0
 * O    PosX/PosY  O 
 * O        X      O 
 * O        X      O
 * O        X      O
 * O        O      O
 * 
 * rotPos 1
 * O        O      O     O    O
 * O    PosX/PosY  X     X    X
 * O        O      O     O    O
 */
    private static final int figureColor = 4;
        

    @Override
    protected void draw(ImageMatrix playground, boolean transparent) {
        int drawColor = (transparent)? 0 : figureColor;
        if (rotPos == 0) {
            playground.setMatrixValue(posX, posY, drawColor);
            playground.setMatrixValue(posX, posY+1, drawColor);
            playground.setMatrixValue(posX, posY+2, drawColor);
            playground.setMatrixValue(posX, posY+3, drawColor);
        } else {
            playground.setMatrixValue(posX, posY, drawColor);
            playground.setMatrixValue(posX+1, posY, drawColor);
            playground.setMatrixValue(posX+2, posY, drawColor);
            playground.setMatrixValue(posX+3, posY, drawColor);
        }
    }

    @Override
    public boolean goDown(ImageMatrix playground) {
        if (rotPos == 0) {
            if (posY + 4 == playground.getSizeY() || playground.getMatrixValue(posX, posY+4) != 0) {
                return false;
            }
        } else {
            if (posY + 1 == playground.getSizeY() ||
               (playground.getMatrixValue(posX, posY+1) != 0 || playground.getMatrixValue(posX+1, posY+1) != 0 || playground.getMatrixValue(posX+2, posY+1) != 0 ||
                playground.getMatrixValue(posX+3, posY+1) != 0)) {
                return false;
            }
        }
        
        draw(playground, true);
        posY += 1;
        draw(playground, false);
        
        return true;
    }

    @Override
    public void goLeft(ImageMatrix playground) {
        if (rotPos == 0) {
            if (posX - 1 < 0 || (playground.getMatrixValue(posX-1, posY) != 0 || playground.getMatrixValue(posX-1, posY+1) != 0 || playground.getMatrixValue(posX-1, posY+2) != 0 ||
                                 playground.getMatrixValue(posX-1, posY+3) != 0)) {
                return;
            }
        } else {
            if (posX - 1 < 0 || playground.getMatrixValue(posX-1, posY) != 0) {
                return;
            }
        }
        draw(playground, true);
        posX -= 1;
        draw(playground, false);
    }

    @Override
    public void goRight(ImageMatrix playground) {
        if (rotPos == 0) {
            if (posX + 1 == playground.getSizeX() ||
               (playground.getMatrixValue(posX+1, posY) != 0 || playground.getMatrixValue(posX+1, posY+1) != 0 || playground.getMatrixValue(posX+1, posY+2) != 0 ||
                playground.getMatrixValue(posX+1, posY+3) != 0)) {
                return;
            }
        } else {
            if (posX + 4 == playground.getSizeX() || playground.getMatrixValue(posX+4, posY) != 0) {
                return;
            }
        }
        draw(playground, true);
        posX += 1;
        draw(playground, false);
    }

    @Override
    public boolean init(ImageMatrix playground) {
        posX = playground.getSizeX() / 2 - 1;
        posY = 0;
        rotPos = 0;
        if (playground.getMatrixValue(posX, posY) != 0 || playground.getMatrixValue(posX, posY+1) != 0 || playground.getMatrixValue(posX, posY+2) != 0 ||
                                                          playground.getMatrixValue(posX, posY+3) != 0) {
            return false;
        }
        draw(playground, false);
        return true;
    }

    @Override
    public void rotate(ImageMatrix playground) {
        if (rotPos == 0) {
            if (posX + 3 >= playground.getSizeX() || (playground.getMatrixValue(posX+1, posY) != 0 || playground.getMatrixValue(posX+2, posY) != 0 ||
                                                      playground.getMatrixValue(posX+3, posY) != 0)) {
                return;
            }
        } else {
            if (posY + 3 >= playground.getSizeY() || (playground.getMatrixValue(posX, posY+1) != 0 || playground.getMatrixValue(posX, posY+2) != 0 ||
                                                      playground.getMatrixValue(posX, posY+3) != 0)) {
                return;
            }
        }
        draw(playground, true);
        rotPos = (rotPos == 0)? 1 : 0;
        draw(playground, false);
    }
}
